
#include <stdlib.h>
#include "cmditeminfo.h"
#include "commmsg.h"
#include "../mainctrl.h"
#include "../cmd/dataprepare.h"
#include "../partner/CUserPartner.h"
#include "../storage/CUserStorage.h"

CT_REGISTER_ASYNCCMD(ID_MAIN_GET_ITEM_DETAIL_INFO , CCmdGetItemDetailInfo);

int CCmdGetItemDetailInfo::Do(void* pData)
{
    CT_ASSERT(pData);

    //解析消息
    CCmdPBDoParam* pstParam = (CCmdPBDoParam*) pData;
    pstMainCtrl = (CMainCtrl*) pstParam->m_pMain;

    if (pstParam->m_pstMsgHead->msgid() != ID_MAIN_GET_ITEM_DETAIL_INFO ||
        pstParam->m_pstMsgHead->msgtype() != CSMsgDef::EMT_REQUEST)
    {
        CT_ERROR(("Cmd Stat Err Cmd=%d CmdID=%d Tpye=%d",
            ID_MAIN_GET_ITEM_DETAIL_INFO,
            pstParam->m_pstMsgHead->msgid(),
            pstParam->m_pstMsgHead->msgtype()));
        return Done(RET_SYS_ERROR);
    }

    stMsgHead = *(pstParam->m_pstMsgHead);
    stEnpHead = *(pstParam->m_pstEnpHead);
    int iRet = pstMainCtrl->DecodePBMsg(stReq, pstParam);
    if (iRet)
    {
        return AnsClients(iRet);
    }
    //消息解析结束

    uiGID = stReq.gid();

    //检查登录
    iRet = pstMainCtrl->CheckLogin(uiGID, stEnpHead);
    if (iRet != RET_OK)
    {
        return AnsClients(iRet);
    }

    LogicDo(RET_OK);
    return RET_OK;
}

int CCmdGetItemDetailInfo::LogicDo(int iRet)
{
    if (iRet != 0)
    {
        return AnsClients(iRet);
    }

    PREPARE_PLAYER_BASE_INFO(this, uiGID);
    PREPARE_PLAYER_DETAIL_INFO(this, uiGID);
    ASSERT_PLAYER_DETAIL_PREPARED(uiGID);
    PREPARE_PLAYER_ONLINE_DATA(this, uiGID);

    int iIndex = pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("get user player base info error, uid = %u", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }
    CPlayerBaseInfo& stPlayerBaseInfo = pstMainCtrl->m_stPlayerBaseInfoHash[iIndex];

    if (stPlayerBaseInfo._ucIsInited != 1)
    {
        return AnsClients(ERROR_NOT_INITED);
    }

    iIndex = pstMainCtrl->m_stPlayerDetailInfoHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        return AnsClients(RET_NO_USER);
    }
    CPlayerDetailInfo& stPlayerDetailInfo = pstMainCtrl->m_stPlayerDetailInfoHash[iIndex];

    iIndex = pstMainCtrl->m_stPlayerOnlineDataHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        return AnsClients(RET_NO_USER);
    }
    CPlayerOnlineData& stPlayerOnlineData = pstMainCtrl->m_stPlayerOnlineDataHash[iIndex];

    ::google::protobuf::RepeatedField<int64> stReqKeys;
    for (int i = 0; i < stReq.keys_size(); i++)
    {  
        int64 i64Tmp = CTStringUtils::StringToInt64 (stReq.keys().Get(i).c_str(), 10);
        stReqKeys.Add(i64Tmp);
    }

    unsigned char ucItemType = stReq.item_type();
    switch(ucItemType)
    {
    case ITEM_TYPE_EQUIP:
        pstMainCtrl->m_stEquipMgr.SendEquipDetailInfoToFront(stPlayerOnlineData, stReqKeys, stMsgHead, stEnpHead);
        break;
    case ITEM_TYPE_SCROLL:
        {
            if (1 != stReq.keys_size())
            {
                 return AnsClients(ERROR_ITEM_INVALID_ITEM_TYPE);

            }
            unsigned short usScrollId = stReqKeys.Get(0);
            pstMainCtrl->m_stStorageMgr.SendScrollDetailInfoToFront(stPlayerOnlineData, usScrollId,stMsgHead, stEnpHead);
        }
        break;
    case ITEM_TYPE_CARD:
        pstMainCtrl->m_stCardMgr.SendCardDetailInfoToFront(stPlayerOnlineData._stUserCard, stPlayerDetailInfo, stReqKeys, stMsgHead, stEnpHead);
        break;
    case ITEM_TYPE_TREASURE:
        pstMainCtrl->m_stTreasureMgr.SendDetailTreasureToFront(stPlayerDetailInfo, stReqKeys);
        break;
    default:
        return AnsClients(ERROR_ITEM_INVALID_ITEM_TYPE);
    }

    return AnsClients(RET_OK);
}

int CCmdGetItemDetailInfo::AnsClients(int iRet)
{
    stMsgHead.set_msgret(iRet);

    if (iRet != RET_OK)
    {
        pstMainCtrl->ReturnMsgHead(stMsgHead, stEnpHead);
        return Done(iRet);
    }

    pstMainCtrl->FlushSendCache();

    return Done(iRet);
}

